package com.xhwl.centiotdata.service.impl;

import com.xhwl.centiotdata.service.IPassengerFlowsNotificationCallbackService;
import com.xhwl.centiotdata.service.cent.IBiPassengerFlowsCentService;
import com.xhwl.centiotdata.service.cent.IDeviceCentService;
import com.xhwl.centiotdata.service.cent.IFaceSkillCentService;
import com.xhwl.centiotdata.service.cent.IPedestrianCentService;
import com.xhwl.common.dto.cent.face.SearchFaceDTO;
import com.xhwl.common.enums.CodeEnum;
import com.xhwl.common.exception.BusinessException;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.device.Device;
import com.xhwl.common.vo.cent.face.FaceSearchVO;
import com.xhwl.common.vo.cent.passengerflow.PassengerFaceFlowCallbackVO;
import com.xhwl.common.vo.cent.passengerflow.PassengerFlowNotificationCallbackVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author lusong
 * @description
 * @date 2021/12/23
 */
@Slf4j
@Service
public class PassengerFlowsNotificationCallbackServiceImpl implements IPassengerFlowsNotificationCallbackService {

//    private final static String urlPrefix = "http://139.159.141.233:8099/";

    @Autowired
    IBiPassengerFlowsCentService biPassengerFlowsCentService;

    @Autowired
    IDeviceCentService deviceCentService;

    @Autowired
    IFaceSkillCentService faceSkillService;

    @Autowired
    IPedestrianCentService pedestrianCentService;

    @Override
    public ResultJson handleNotification(PassengerFlowNotificationCallbackVO callbackVO) {
        //对于华为3800来说，上报过来的id，其实是设备中台thirdDeviceId的 前缀20个字符
        String idPrefix = callbackVO.getDeviceId();
        Device device = deviceCentService.getByThirdIdPrefix(idPrefix);
        if (Objects.isNull(device)) {
            return ResultJson.fail("中台不存在设备id：" + callbackVO.getDeviceId());
        }
        callbackVO.setDeviceCentId(device.getId());
        callbackVO.setThirdDeviceId(device.getThirdDeviceId());
        callbackVO.setIotDeviceId(device.getIotDeviceId());
        ResultJson resultJson = biPassengerFlowsCentService.saveNotification(callbackVO);
        log.debug("{},发送到bi：{}", resultJson.getMessage(), callbackVO);
        return ResultJson.success();
    }

    @Override
    public ResultJson handleFlowFace(PassengerFaceFlowCallbackVO callbackVO) {

        String idPrefix = callbackVO.getDeviceId();
        Device device = deviceCentService.getByThirdIdPrefix(idPrefix);
        if (Objects.isNull(device)) {
            return ResultJson.fail("中台不存在设备id：" + callbackVO.getDeviceId());
        }
        callbackVO.setDeviceName(device.getName());
        callbackVO.setProjectId(device.getProjectId());
        callbackVO.setDeviceCentId(device.getId());
        callbackVO.setThirdDeviceId(device.getThirdDeviceId());
        callbackVO.setIotDeviceId(device.getIotDeviceId());

        return pedestrianCentService.handleFlowFace(callbackVO);
    }
}
